package com.free.bsf;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.FileType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.sun.jndi.toolkit.url.Uri;

import java.io.*;
import java.net.URL;
import java.util.Properties;

public class CodeGenerator {

    public static void main(String[] args)  {
        try {
            Properties properties = loadConfig();
            String output_dir = properties.getProperty("output_dir");
            String author = properties.getProperty("author");
            String jdbc_url = properties.getProperty("jdbc_url");
            String jdbc_driver = properties.getProperty("jdbc_driver");
            String jdbc_username = properties.getProperty("jdbc_username");
            String jdbc_password = properties.getProperty("jdbc_password");
            String parent_package = properties.getProperty("parent_package");
            String entity_package = properties.getProperty("entity_package");
            String mapper_package = properties.getProperty("mapper_package");
            String xml_package = properties.getProperty("xml_package");
            String swagger = properties.getProperty("swagger");
            String tables = properties.getProperty("tables");
            AutoGenerator mpg = new AutoGenerator();
            GlobalConfig gc = new GlobalConfig();
            gc.setOutputDir(output_dir);
            gc.setOpen(false);
            gc.setFileOverride(true);
            gc.setServiceName("%sService");
            gc.setIdType(IdType.AUTO);
            gc.setDateType(DateType.TIME_PACK);
            gc.setSwagger2(Boolean.parseBoolean(swagger));
            gc.setAuthor(author);
            mpg.setGlobalConfig(gc);
            //controller,service,service_impl 跳过目录
            mpg.setCfg((new InjectionConfig() {
                public void initMap() {
                }
            }).setFileCreate((configBuilder, fileType, filePath) -> (FileType.CONTROLLER != fileType && FileType.SERVICE != fileType && FileType.SERVICE_IMPL != fileType)));
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl(jdbc_url);
            dsc.setDriverName(jdbc_driver);
            dsc.setUsername(jdbc_username);
            dsc.setPassword(jdbc_password);
            if (jdbc_driver.toLowerCase().contains("mysql"))
                dsc.setDbType(DbType.MYSQL);
            else if (jdbc_driver.toLowerCase().contains("postgresql")) {
                dsc.setDbType(DbType.POSTGRE_SQL);
            }
            mpg.setDataSource(dsc);
            PackageConfig pc = new PackageConfig();
            pc.setParent(parent_package);
            pc.setController("");
            pc.setService("");
            pc.setServiceImpl("");
            pc.setEntity(entity_package);
            pc.setMapper(mapper_package);
            pc.setXml(xml_package);
            mpg.setPackageInfo(pc);
            StrategyConfig strategy = new StrategyConfig();
            if (!StringUtils.isEmpty(tables))
                strategy.setInclude(tables.split(","));
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
            strategy.setEntityLombokModel(true);
            mpg.setStrategy(strategy);
            mpg.execute();
        }catch (Exception e){
            System.err.println(e);
        }
    }

    public static Properties loadConfig() throws IOException {
        String files="conf/config.properties,config.properties,C:\\working\\mybatis-plus-toolkit\\doc\\conf\\config.properties";
        for(String file:files.split(",")) {
            File f = new File(file);
            if (f.exists()) {
                try(InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
                    Properties properties = new Properties();
                    properties.load(inputStream);
                    return properties;
                }
            }
        }
        throw new RuntimeException("找不到配置文件config.properties");
    }

}
